package com.tust.app.controller;



import com.github.pagehelper.PageInfo;
import com.tust.app.domain.Answer;
import com.tust.app.domain.Topic;
import com.tust.app.service.TopicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
public class TopicController {

    @Autowired
    private TopicService topicService;

    // 获取所有帖子
    @GetMapping("/selectAllTopic3")
    public String selectAllTopic(@RequestParam(value = "pn", defaultValue = "1") int pageNum, Model model) {
        PageInfo pageInfo = topicService.getAllTopics(pageNum);
        model.addAttribute("topicLists", pageInfo);
        return "admin/bbs/allTopic";  // 返回视图名称，对应 JSP 文件
    }

    @GetMapping("/selectAllTopic2")
    public String selectAllTopic2(@RequestParam(value = "pn", defaultValue = "1") int pageNum, Model model) {
        PageInfo pageInfo = topicService.getAllTopics(pageNum);
        model.addAttribute("topicLists", pageInfo);
        return "frontPage/messageList";  // 返回视图名称，对应 JSP 文件
    }

    @GetMapping("/selectTopicAnswerById")
    public String selectTopicAnswerById(@RequestParam("id") int id, Model model) {
        Topic topic = topicService.selectTopicAnswerById(id);
        List<Answer> list=topicService.getAnswerList(id);
        model.addAttribute("list", topic);
        model.addAttribute("username", "");
        model.addAttribute("lists", list);
        model.addAttribute("replyNum", list.size());
        return "frontPage/detail";
    }

    // 更新帖子状态
    @GetMapping("/updateState")
    public String updateState(@RequestParam("id") int id,Model model) {
        topicService.updateTopicState(id);
        return "redirect:/selectAllTopic3";
    }

    // 删除帖子
    @GetMapping("/deleteTopic")
    public String deleteTopic(@RequestParam("id") int id,@RequestParam("type") int type,Model model) {
        topicService.deleteTopic(id);
        if (type==1){
            return "redirect:/selectAllTopic";

        }
        return "redirect:/selectAllTopic3";

    }
    @PostMapping("/addTopic")
    public String addTopic(@RequestParam("edituser") String edituser,
                           @RequestParam("title") String title,
                           @RequestParam("detail") String detail,
                           Model model) {
        // 处理表单数据
        topicService.addTopic(edituser, title, detail);

        // 重定向到成功页面或其他页面
        return "frontPage/addtopic";
    }


    @GetMapping("/selectAllTopic")
    public String selectAllTopic(@RequestParam(value = "pn", defaultValue = "1") int pageNum,@RequestParam("edituser") String edituser, Model model) {
        // 获取所有帖子数据
        if (edituser!=null){
            PageInfo pageInfo = topicService.getAllTopicsByUser(edituser,pageNum);

            // 将数据添加到模型中
            model.addAttribute("topicList", pageInfo);
            model.addAttribute("edituser", edituser);
        }


        // 返回视图名称
        return "frontPage/topicList";
    }


    @PostMapping("/editTopic")
    public String updateTopic(@RequestParam("id") int id,
                              @RequestParam("title") String title,
                              @RequestParam("detail") String detail) {
        // 更新帖子数据
        topicService.updateTopic(id, title, detail);

        return "redirect:/selectAllTopic3"; // 重定向到帖子列表页面
    }


    @GetMapping("selectTopicById")
    public String selectTopicById(@RequestParam("id") int id,Model model){
        Topic topic=topicService.getTopicById(id);
        model.addAttribute("topic",topic);
        return "frontPage/edittopic";
    }
}
